package com.example.demo040801.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import com.example.demo040801.R;

import org.jetbrains.annotations.Nullable;

public class ImageActivity extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        imageView = findViewById(R.id.imageView);
        String imageUrl = getIntent().getStringExtra("image_url");
        imageView.setTag(imageUrl);

        Glide.with(this)
                .asBitmap()
                .load(imageUrl)
                .listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        imageView.setImageBitmap(resource);
                        imageView.setOnLongClickListener(v -> {
                            String imageUrlFromTag = (String) v.getTag();
                            showSaveDialog(imageUrlFromTag);
                            return true;
                        });
                        return true;
                    }
                })
                .into(imageView);
    }

    private void showSaveDialog(String imageUrl) {
        new AlertDialog.Builder(this)
                .setTitle("保存图片")
                .setMessage("你想把这个图片保存到你的相册吗？")
                .setPositiveButton("保存", (dialog, which) -> saveImage(imageUrl))
                .setNegativeButton("取消", null)
                .show();
    }

    private void saveImage(String imageUrl) {
        Glide.with(this)
                .asBitmap()
                .load(imageUrl)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        saveBitmapToGallery(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }
                });
    }

    private void saveBitmapToGallery(Bitmap bitmap) {
        // Implement the logic to save the bitmap to the gallery
        // After saving, show a Toast
        Toast.makeText(this, "Image saved successfully", Toast.LENGTH_SHORT).show();
    }
}